//package com.lr.transfer.config;
//
//import com.alibaba.fastjson.JSONObject;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import java.io.IOException;
//import java.util.Enumeration;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * 过滤器配置
// */
////@Configuration
////public class ConfigurationFilter {
////    private final static Logger logger = LoggerFactory.getLogger(ConfigurationFilter.class);
////    @Autowired
////    private HttpHandle httpHandle;
////
////    @Bean
////    public FilterRegistrationBean filterRegistrationBean() {
////        FilterRegistrationBean registration = new FilterRegistrationBean();
////        registration.setFilter(new ApiRepFilter());//添加过滤器
////        registration.addUrlPatterns("/**");//设置司法处置模块对外api的过滤路径，/*所有路径
//////        registration.addUrlPatterns("/api/v1/OJD/photocopyContract/*");//设置影印合同模块对外api的过滤路径，/*所有路径
//////        registration.addUrlPatterns("/api/v1/OJD/customerInfo/*");//设置用户管理模块对外api的过滤路径，/*所有路径
//////        registration.addUrlPatterns("/api/v1/OJD/electronicContract/*");//设置电子合同模块对外api的过滤路径，/*所有路径
//////        registration.addInitParameter("name", "alue");//添加默认参数
////        registration.setName("ApiRepFilter");//设置优先级
////        registration.setOrder(1);//设置优先级
////        return registration;
////    }
//
///**
// * 对外api请求过滤器
// * 验证数据合法性
// * 每一个接入方云证金融逾期督促平台都会生成一个userCode和key
// * utime为系统时间戳
// * sign生成方式：
// * userCode+key+utime+json组成一个新的字符串，然后计算生成新字符串的md5值做为sign的验证
// */
//@Component
//@WebFilter(urlPatterns = "/lr/MD5Transfer", filterName = "authFilter")
//public class ApiRepFilter implements Filter {
//    @Autowired
//    private HttpHandle httpHandle;
//    private final static Logger logger = LoggerFactory.getLogger(ApiRepFilter.class);
//
//    @Override
//    public void destroy() {
//    }
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain
//            filterChain)
//            throws ServletException, NumberFormatException, IOException {
//        logger.info("---------------------开始对外api请求过滤----------------------------");
//        ServletRequest requestWrapper = null;
//        if (servletRequest instanceof HttpServletRequest) {
//            requestWrapper = new BodyReaderHttpServletRequestWrapper((HttpServletRequest) servletRequest);
//        }
//
//        // 获取请求头数据
////        JSONObject msg = new JSONObject();
////        Map<String, String> headerInfo = new HashMap<>();
////            Enumeration<String> headerNames = ((BodyReaderHttpServletRequestWrapper) requestWrapper).getHeaderNames();
////            if (headerNames != null) {
////                while (headerNames.hasMoreElements()) {
////                    String headerName = headerNames.nextElement();
////                    String headerValue = ((BodyReaderHttpServletRequestWrapper) requestWrapper).getHeader(headerName);
////                    headerInfo.put(headerName, headerValue);
////                }
////            }
////            String userCode = headerInfo.get("usercode");
////            logger.info("userCode:" + userCode);
////            String sign = headerInfo.get("sign");
////            logger.info("摘要:" + sign);
////            String utime = headerInfo.get("utime");
////            logger.info("时间戳:" + utime);
//
////            // 检查签名是否为空
////            if (StringUtils.isEmpty(userCode) ||
////                    StringUtils.isEmpty(sign) ||
////                    StringUtils.isEmpty(utime)) {
////                msg.put("resultCode", ResultCode.SIGN_DATA_EMPTY_ERROR.getCode());
////                msg.put("resultMsg", ResultCode.SIGN_DATA_EMPTY_ERROR.getMsg());
////                msg.put("data", new JSONObject());
////                httpHandle.jsonContent(msg.toJSONString(), servletResponse);
////                return;
////            }
//
////            // 检查用户是否存在
////            long startTime = System.nanoTime();  //开始时间
////            CustomerInfo customerInfo = customerInfoService.findByCustomId(Long.parseLong(userCode));
////            long consumingTime = System.nanoTime() - startTime; //消耗时间
////            logger.info("用户检查运行时间：" + (consumingTime/1000000) + "ms");
////            if (customerInfo == null) {
////                msg.put("resultCode", ResultCode.ILLEGAL_USER_ERROR.getCode());
////                msg.put("resultMsg", ResultCode.ILLEGAL_USER_ERROR.getMsg());
////                msg.put("data", new JSONObject());
////                httpHandle.jsonContent(msg.toJSONString(), servletResponse);
////                return;
////            }
//
//
//        if (null != requestWrapper) {
//            String jsonContent = httpHandle.readJSONString(requestWrapper.getReader());
//            servletRequest.setAttribute("jsonContent", jsonContent);
////                requestWrapper.getInputStream();
////                String publicKeyCode = customerInfo.getUserKey();
////                String value = userCode + publicKeyCode + utime + jsonContent;
////                String md5Value = MD5Util.MD5Encoder(value);
////                if (!(md5Value.toLowerCase()).
////                        equals(sign.toLowerCase())) {
////                    msg.put("resultCode", ResultCode.SIGN_DATA_VARIFY_ERROR.getCode());
////                    msg.put("resultMsg", ResultCode.SIGN_DATA_VARIFY_ERROR.getMsg());
////                    msg.put("data", new JSONObject());
////                    httpHandle.jsonContent(msg.toJSONString(), servletResponse);
////                    return;
////                }
////            } else {
////                msg.put("resultCode", ResultCode.SIGN_DATA_VARIFY_ERROR.getCode());
////                msg.put("resultMsg", ResultCode.SIGN_DATA_VARIFY_ERROR.getMsg());
////                msg.put("data", new JSONObject());
////                httpHandle.jsonContent(msg.toJSONString(), servletResponse);
////                return;
//        }
//        filterChain.doFilter(requestWrapper, servletResponse);
//    }
//
//    @Override
//    public void init(FilterConfig arg0) throws ServletException {
//    }
//}
////}
